﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using UIWindows.Tools.Helper;
using XingLucifer.IBase.Enums;

namespace UIWindows.Tools.Converter
{
    [ValueConversion(typeof(long), typeof(DateTime))]
    public class TimeStampToDateTimeStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                Debug.WriteLine(value.ToString());
                DateTime dDate;
                //long age = (long)value;
                if (DateTime.TryParseExact(value.ToString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
                {
                    return dDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (DateTime.TryParseExact(value.ToString(), "yyyy/M/d H:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
                {
                    return dDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (DateTime.TryParseExact(value.ToString(), "yyyy/M/dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
                {
                    return dDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (DateTime.TryParseExact(value.ToString(), "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
                {
                    return dDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (DateTime.TryParseExact(value.ToString(), "yyyy/MM/d HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
                {
                    return dDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
                if (DateTime.TryParseExact(value.ToString(), "yyyy/MM/d H:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate))
                {
                    return dDate.ToString("yyyy-MM-dd HH:mm:ss");
                }
             
               
                return new DateTime(0001, 1, 1);
                //return age == 0 ? new DateTime(0001, 1, 1) : age.GetDateTime();
                //return age == 0 ? "0000-00-00 00:00:00" : age.GetDateTime();
            }
            catch (Exception)
            {
                return new DateTime(0001, 1, 1);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ScanPositionTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                int number = System.Convert.ToInt32(value);
                var a = (ScanPositionType)number;
                return a.ToString();
                //return age == 0 ? "0000-00-00 00:00:00" : age.GetDateTime();
            }
            catch (Exception)
            {
                return new DateTime(0001, 1, 1);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class MachineStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                string result = "";
                //设备状态 0:待机、1:运行、2:故障、3:报警(运行,停止,报警)
                switch (value.ToString())
                {
                    case "0":
                        result = "待机";
                        break;
                    case "1":
                        result = "运行中";
                        break;
                    case "2":
                        result = "故障";
                        break;
                    case "3":
                        result = "报警";
                        break;
                    default: result="未连接";break;
                }
                return result;
     
            }
            catch (Exception)
            {
                return new DateTime(0001, 1, 1);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
